package com.hqyj.javaSpringBoot.modules.account.controller;

import com.github.pagehelper.PageInfo;
import com.hqyj.javaSpringBoot.modules.account.entity.Role;
import com.hqyj.javaSpringBoot.modules.account.service.RoleService;
import com.hqyj.javaSpringBoot.modules.common.entity.Result;
import com.hqyj.javaSpringBoot.modules.common.entity.SearchBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 通过 id 获取 role 接口
     * 127.0.0.1:8080/api/role/{id} ---- get
     */
    @GetMapping("/role/{id}")
    public Role getRoleById(@PathVariable int id){
        return roleService.getRoleById(id);
    }

    /**
     * 获取所有角色接口
     * 127.0.0.1:8080/api/roles ---- get
     */
    @GetMapping("/roles")
    public List<Role> getRoles(){
        return roleService.getRoles();
    }

    /**
     * 新增角色接口
     * 127.0.0.1:8080/api/role ---- post
     * {"roleName":"admin1"}
     */
    @PostMapping(value = "/role", consumes = "application/json")
    public Result<Role> insertRole(@RequestBody Role role){
        return roleService.insertRole(role);
    }

    /**
     * 更新角色信息接口
     * 127.0.0.1:8080/api/role ---- put
     * {"id":4,"roleName":"admin2"}
     */
    @PutMapping(value = "/role",consumes = "application/json")
    public Result<Role> updateRole(@RequestBody Role role){
        return roleService.updateRole(role);
    }

    /**
     * 删除角色 接口
     * 127.0.0.1:8080/api/role/{id} ---- delete
     */
    @DeleteMapping("/role/{id}")
    public Result<Object> deleteRoleById(@PathVariable int id){
        return roleService.deleteRoleById(id);
    }

    /**
     * role 多条件分页查询接口
     * 127.0.0.1:8080/api/roles ---- post
     * {"currentPage":"1","pageSize":"5","orderBy":"id","direction":"desc","keyWord":""}
     */
    @PostMapping(value = "/roles", consumes = "application/json")
    public PageInfo<Role> getRolesBySearchBean(@RequestBody SearchBean searchBean){
        return roleService.getRolesBySearchBean(searchBean);
    }
}
